<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Mixins Demo</title>
	
	<script src="../common/log.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../dist/inheritance.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>
  <p>Ruby style mixins</p>
  
  View page source...
  <hr/>
  
  <div id="log"><!-- logging will appear in here --></div>
  <script type="text/javascript" language="javascript" charset="utf-8">
  // <![CDATA[
    Comparable = {
      greaterThan: function(other) {
        return (this.compareTo(other) == 1);
      },
      
      lessThan: function(other) {
        return (this.compareTo(other) == -1);
      },
      
      greaterThanEqualTo: function(other) {
        return (this.compareTo(other) >= 0);
      },
      
      lessThanEqualTo: function(other) {
        return (this.compareTo(other) <= 0);
      },
      
      equals: function(other) {
        return (this.compareTo(other) == 0);
      },
      
      compareTo: function(other) {
        // expects this to be implemented and return -1, 0, 1 for
        // less than, equal to, and greater than respectively
      }
    };
    
    
    Employee = Class.extend({
      
      include: Comparable,
      
      initialize: function(name, dept) {
        this.name = name;
        this.dept = dept;
      },
      
      compareTo: function(other) {
        if (this.name > other.name) {
          return 1;
        } else if (this.name < other.name) {
          return -1;
        } else {
          return 0;
        }
      },
      
      toString: function() {
        return '[' + this.name + ', ' + this.dept + ']';
      }
    });
    
    var joe = new Employee("Joe", "Sales");
    var bob = new Employee("Bob", "Support");
    
    log("joe.greaterThan(bob) = " + joe.greaterThan(bob));
    log("joe.lessThan(bob) = " + joe.lessThan(bob));
    log("joe.greaterThanEqualTo(bob) = " + joe.greaterThanEqualTo(bob));
    log("joe.lessThanEqualTo(bob) = " + joe.lessThanEqualTo(bob));
    log("joe.equals(bob) = " + joe.equals(bob));
    log("joe.compareTo(bob) = " + joe.compareTo(bob));
    
    // 2 empty lines
    log();log();
    
    var tyler = new Employee("Tyler", "IT");
    var sue = new Employee("Sue", "IT");
    var mary = new Employee("Mary", "Support");
    var joe2 = new Employee("Joe", "IT");
    
    var employees = [tyler, sue, joe2, mary, joe, bob];
        
    function bubbleSort(arr) {
      var x, y, temp;
      // The Bubble Sort method.

      for(x = 0; x < arr.length; x++) {
        for(y = 0; y < (arr.length-1); y++) {
          if(arr[y].greaterThan(arr[y+1])) {
            temp = arr[y+1];
            arr[y+1] = arr[y];
            arr[y] = temp;
          }
        }
      }
      
      return arr;
    }

    log('[' + employees + ']');
    log('[' + bubbleSort(employees) + ']');
    
  // ]]>
  </script>
</body>
</html>
